﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using MethodWorx.CMS.UI.Application;
using MethodWorx.CMS.UI.Controllers;
using MethodWorx.CMS.UI.Models;
using MethodWorx.CMS.Helpers;
using MethodWorx.CMS.Plugins;
using MethodWorx.CMS.Content;
using MethodWorx.Core.Mvc;
using StructureMap;
using MethodWorx.Core.DomainModel;
using MethodWorx.CMS.Requests;
using System.Data.Linq;

namespace MethodWorx.CMS.UI
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Plugins",
                "Plugin/{type}/{extensionId}/{instance}/{action}", new { controller = "Plugin", action = "Invoke" });

            routes.MapRoute(
                "GlobalPlugins",
                "GlobalPlugin/{extensionId}/{action}/{id}", new { controller = "GlobalPlugin", action = "Invoke", id = UrlParameter.Optional});

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional });

            routes.MapRoute(
                "EditPageProperty",
                "Page/EditProperty/{id}/{name}", new { controller = "Page", action="EditProperty"});

            routes.MapRoute(
                "EditConentItemProperty",
                "Page/EditProperty/{id}/{contentItemId}/{name}", new { controller = "Page", action = "EditContentItemProperty" });
        }

        protected void Application_Start()
        {
            System.Web.Hosting.HostingEnvironment.RegisterVirtualPathProvider(new AssemblyResourceProvider()); 

            AreaRegistration.RegisterAllAreas();

            RegisterRoutes(RouteTable.Routes);

            BootStrapper.ConfigureStructureMap();
            ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory());
            var registry = new StructureMapRepositoryRegistry();

            ModelBinders.Binders.Add(typeof(PropertyDetails<object>), new PropertyDetailsModelBinder());
            ModelBinders.Binders.Add(typeof(PageTemplate), new RepositoryModelBinder<PageTemplate>(registry));
            ModelBinders.Binders.Add(typeof(Page), new RepositoryModelBinder<Page>(registry));
            ModelBinders.Binders.Add(typeof(ContentTemplate), new RepositoryModelBinder<ContentTemplate>(registry));
            ModelBinders.Binders.Add(typeof(ContentItem), new RepositoryModelBinder<ContentItem>(registry));
        }

        protected void Application_EndRequest()
        {
            ObjectFactory.ReleaseAndDisposeAllHttpScopedObjects();

        }

        protected void Application_BeginRequest()
        {
           
        }
    }
}
